package main.api;

import main.RequestHelper;
import main.ResponseHelper;
import main.TimeHelper;
import main.database.BookCommentHelper;
import org.json.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static main.RealPersonHelper.rpHash;

@RestController
public class BookCommentApi {
    @PostMapping ("/api/submitbookcomment")
    public ResponseEntity<?> submitComment(HttpServletRequest request) {
        String result;

        String uuid = RequestHelper.getUserUUIDFromRequest(request);
        if (uuid == null || uuid.isEmpty()) {
            result = ResponseHelper.createBasicJsonObject(404, "登录用户才能发布书评").toString();
        }
        else {
            String data = null, title = null, id = null, clientHash = null, captcha = null;
            Map<String, String[]> map = request.getParameterMap();
            if (map.containsKey("data")) {
                data = map.get("data")[0];
            }
            if (map.containsKey("title")) {
                title = map.get("title")[0];
            }
            if (map.containsKey("id")) {
                id = map.get("id")[0].trim();
            }
            if (map.containsKey("clientHash")) {
                clientHash = map.get("clientHash")[0];
            }
            if (map.containsKey("captcha")) {
                captcha = map.get("captcha")[0];
            }

            if (data == null || data.isEmpty() || data.length() < 300) {
                result = ResponseHelper.createBasicJsonObject(404, "评书内容必须超过300个字").toString();
            }
            else if (title.trim().isEmpty()) {
                result = ResponseHelper.createBasicJsonObject(404, "书评必须给到题目").toString();
            }
            // ID为空，说明是一篇新书评，我们必须在这里拦住，防止自动刷单的
            else if (id.trim().isEmpty() && (clientHash == null || clientHash.isEmpty())) {
                result = ResponseHelper.createBasicJsonObject(404, "没有提供图形验证码信息").toString();
            }
            else {
                if (id.trim().isEmpty()) {
                    String rpHash = rpHash(captcha);

                    if (rpHash.equals(clientHash)) {
                        id = BookCommentHelper.submit(uuid, title, data, true, id);
                        JSONObject jo = ResponseHelper.createBasicJsonObject(200, "ok");
                        jo.put("id", id);
                        result = jo.toString();
                    } else {
                        result = ResponseHelper.createBasicJsonObject(404, "验证码错误").toString();
                    }
                }
                else {
                    BookCommentHelper.submit(uuid, title, data, true, id);
                    JSONObject jo = ResponseHelper.createBasicJsonObject(200, "ok");
                    jo.put("id", id);
                    result = jo.toString();
                }
            }
        }

        return ResponseEntity.ok(result);
    }

    @PostMapping("/api/savebookcomment")
    public ResponseEntity<?> saveBookComment(HttpServletRequest request) {
        String result;

        String uuid = RequestHelper.getUserUUIDFromRequest(request);
        if (uuid == null || uuid.isEmpty()) {
            result = ResponseHelper.createBasicJsonObject(404, "登录用户才能发布书评").toString();
        }
        else {
            String data = null, title = null, id = null;
            Map<String, String[]> map = request.getParameterMap();
            if (map.containsKey("data")) {
                data = map.get("data")[0];
            }
            if (map.containsKey("title")) {
                title = map.get("title")[0];
            }
            if (map.containsKey("id")) {
                id = map.get("id")[0].trim();
            }

            if (data.isEmpty()) {
                result = ResponseHelper.createBasicJsonObject(404, "内容是空的").toString();
            }
            else {
                id = BookCommentHelper.save(uuid, title, data, id);
                JSONObject jo = ResponseHelper.createBasicJsonObject(200, "ok");
                jo.put("id", id);
                jo.put("savetime", TimeHelper.getFormattedNowString());
                result = jo.toString();
            }
        }

        return ResponseEntity.ok(result);
    }

    @PostMapping ("/api/updatebookcomment")
    public ResponseEntity<?> updateComment(HttpServletRequest request) {
        String result;

        String uuid = RequestHelper.getUserUUIDFromRequest(request);
        if (uuid == null || uuid.isEmpty()) {
            result = ResponseHelper.createBasicJsonObject(404, "登录用户才能发布书评").toString();
        }
        else {
            String data = null, title = null, id = null;
            Map<String, String[]> map = request.getParameterMap();
            if (map.containsKey("data")) {
                data = map.get("data")[0];
            }
            if (map.containsKey("title")) {
                title = map.get("title")[0];
            }
            if (map.containsKey("id")) {
                id = map.get("id")[0].trim();
            }

            if (data == null || data.isEmpty() || data.length() < 300) {
                result = ResponseHelper.createBasicJsonObject(404, "评书内容必须超过300个字").toString();
            }
            else if (title.trim().isEmpty()) {
                result = ResponseHelper.createBasicJsonObject(404, "书评必须给到题目").toString();
            }
            else {
                id = BookCommentHelper.update(title, data, id);
                JSONObject jo = ResponseHelper.createBasicJsonObject(200, "ok");
                jo.put("id", id);
                result = jo.toString();
            }
        }

        return ResponseEntity.ok(result);
    }
}
